package ThreadDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-11
 * Time: 15:01
 */
public class Demo10 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            //Thread.currentThread()相当于this引用，拿到的是所在线程的对象的引用
            while(!Thread.currentThread().isInterrupted()){
                System.out.println("hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Thread_Demo10");

        thread.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();//该方法除了会把Interrupted置为true，还会唤醒该对象的阻方法，如Thread.sleep()
        //由于thread大部分时间在进行休眠，所以就会唤醒该对象，之后报错
    }
}
